
//THIS CODE FILE PRODUCES TABLE A.7

global main "[YOUR PATH HERE]"
global output "${main}/output"

//switch to rerun the build stacked data do file
local switch_rerun_stacking = 0
local switch_controls = 1

local switch_dd = 1 //this runs the table as a DD in addition to an event study 

********************************************************************************
**CREATE STACKED DATA**
********************************************************************************

if `switch_rerun_stacking'{
	do "${main}/code/build_stacked_data.do"
}

********************************************************************************
**ENROLLMENT DATA**
********************************************************************************
foreach c1 in public fponly all{
	use "${main}/data/outcomes_completions_cip_all.dta", clear
	
	if "`c1'" == "public"{
		keep if control == 1
		local ctrl  "_pubonly"
	}
	else if "`c1'" == "fponly"{
		keep if control == 3
		local ctrl  "_fponly"
	}
	else if "`c1'" == "all"{
		local ctrl = "_all"
	}

	tempfile usingc
		save `usingc', replace
		
	foreach l1 in "cert1" "cert2" "aa"{
		
		use `usingc', clear
		
		if "`l1'" == "cert1"{
			keep if deg == 1
			local lvl  "_c1"
		}
		else if "`l1'" == "cert2"{
			keep if deg == 2
			local lvl  "_c2"
		}
		else if "`l1'" == "aa"{
			keep if deg == 3
			local lvl = "_aa"
		}


		//collapse by state
		collapse (sum) ctotalt ctotalm ctotalw , by(file_year st_fips)
			drop if st_fips > 56 //this drops all of the US territories
			drop if file_year < 1986 // this drops 1980, 1984 and 1985, for which our data are much spottier
			
			
		rename file_year year_academic
		rename st_fips statefips 
		keep year_academic statefips ctotalt ctotalm ctotalw
				
		foreach x in t m w{
			gen log_ctotal`x' = ln(ctotal`x')
		}
			
			
		if `switch_controls'{
			//generate indicators for if a state has a free CC program in a given year
			gen free_cc = 0
				replace free_cc = 1 if statefips == 5  & year_academic > 2016
				replace free_cc = 1 if statefips == 6  & year_academic > 2018
				replace free_cc = 1 if statefips == 9  & year_academic > 2019
				replace free_cc = 1 if statefips == 10 & year_academic > 2005
				replace free_cc = 1 if statefips == 13 & year_academic > 2013
				replace free_cc = 1 if statefips == 15 & year_academic > 2017
				replace free_cc = 1 if statefips == 18 & year_academic > 1990
				replace free_cc = 1 if statefips == 19 & year_academic > 2019
				replace free_cc = 1 if statefips == 20 & year_academic > 2016
				replace free_cc = 1 if statefips == 21 & year_academic > 2016
				replace free_cc = 1 if statefips == 22 & year_academic > 2016
				replace free_cc = 1 if statefips == 24 & year_academic > 2016
				replace free_cc = 1 if statefips == 28 & year_academic > 2016
				replace free_cc = 1 if statefips == 29 & year_academic > 2016
				replace free_cc = 1 if statefips == 30 & year_academic > 2016
				replace free_cc = 1 if statefips == 32 & year_academic > 2016
				replace free_cc = 1 if statefips == 34 & year_academic > 2016
				replace free_cc = 1 if statefips == 36 & year_academic > 2016
				replace free_cc = 1 if statefips == 40 & year_academic > 2016
				replace free_cc = 1 if statefips == 41 & year_academic > 2016
				replace free_cc = 1 if statefips == 44 & year_academic > 2016
				replace free_cc = 1 if statefips == 46 & year_academic > 2016
				replace free_cc = 1 if statefips == 47 & year_academic > 2016
				replace free_cc = 1 if statefips == 53 & year_academic > 2016
				replace free_cc = 1 if statefips == 54 & year_academic > 2016
			
			//add controls for unemployment rate by state
			preserve
				use "${main}/data/clean_controls_labormarket.dta", clear
			
				keep statefips unemp_rate year
					rename year year_academic
				
				tempfile unemployment
				save `unemployment'
			restore 
		
			merge m:1 year_academic statefips using `unemployment', assert(match using) keep(match) nogen 
			//need to assert match master so when we restrict years we can get rid of those not in our sample
			
			//add controls for log state population (18-30 years old)
			preserve
				use "${main}/data/census_yearly_statepop.dta", clear
			
				tempfile statepop
				save `statepop'
			restore 
		
			merge m:1 year_academic statefips using `statepop', assert(match using) keep(match) nogen 
			//need to assert match master so when we restrict years we can get rid of those not in our sample
						
			keep year_academic statefips ctotal* log_ctotal* free_cc unemp_rate lnpop_1830
			
			tempfile complete
				save `complete'
				
			local controls "unemp_rate"
			local cont "_ctrl"
		}

		else{
			local ytitle "log enrollment in college"
			local controls ""
			local cont ""
		}
		

		merge 1:m year_academic statefips using "${main}/data/mw_stacked.dta", keep(match) // keep matches drops years outside of our enrollment panel
		
	********************************************************************************
	**GRAPHING**
	********************************************************************************

	matrix col1`ctrl'`lvl' = J(8,2,.)
	matrix col2`ctrl'`lvl' = J(8,2,.)
	matrix col3`ctrl'`lvl' = J(8,3,.)
	matrix results`ctrl'`lvl' = J(8,7,.)
	
	matrix DD`lvl' = J(20,3,.)

	* define treatment vector
	global treat F*treat L*treat

	*linear combinations of post-reg coeffs
	global lincomm3 F3treat - F1treat
	global lincomm2 F2treat - F1treat
	global lincomm1 F1treat - F1treat
	global lincom0 L0treat - F1treat
	global lincom1 L1treat - F1treat
	global lincom2 L2treat - F1treat
	global lincom3 L3treat - F1treat
	global lincom4 L4treat - F1treat
	
	local d = 1
	local cn = 0
	foreach var in log_ctotalt log_ctotalm log_ctotalw{
		local cn = `cn'+1
		reghdfe `var' $treat `controls' , ///
			a(i.event#i.statefips i.event#i.rel_time i.event#i.control_origeventpost ///
			i.event#control_fedeventpost i.event#control_othereventpost) cluster(dmw_statefips)
				local dof = e(df_r)
				local i = 1
					
			foreach year in 3 2 1{
				lincom (${lincomm`year'})
					matrix col`cn'`ctrl'`lvl'[`i',1] = r(estimate)
					matrix col`cn'`ctrl'`lvl'[`i++',2] = r(se)
			}
			foreach year in 0 1 2 3 4{
				lincom (${lincom`year'})
					matrix col`cn'`ctrl'`lvl'[`i',1] = r(estimate)
					matrix col`cn'`ctrl'`lvl'[`i++',2] = r(se)
			}
			if `switch_dd'{
				lincom ((${lincom0} + ${lincom1} + ${lincom2} + ${lincom3})/4)
					matrix DD`lvl'[`d',1] = r(estimate)
					matrix DD`lvl'[`d++',2] = r(se)
			}
	}
	
	matrix col3`ctrl'`lvl'[1,3] = -3
	matrix col3`ctrl'`lvl'[2,3] = -2
	matrix col3`ctrl'`lvl'[3,3] = -1
	matrix col3`ctrl'`lvl'[4,3] = 0
	matrix col3`ctrl'`lvl'[5,3] = 1
	matrix col3`ctrl'`lvl'[6,3] = 2
	matrix col3`ctrl'`lvl'[7,3] = 3
	matrix col3`ctrl'`lvl'[8,3] = 4

	matrix results`ctrl'`lvl' = col1`ctrl'`lvl', col2`ctrl'`lvl', col3`ctrl'`lvl'	
		
	}


	matrix r`ctrl' = J(8,39,.)
	matrix r`ctrl' = results`ctrl'_c1, results`ctrl'_c2, results`ctrl'_aa
	
	clear
	matlist r`ctrl'
	svmat r`ctrl'
				
	export excel "${output}/tables_raw.xlsx", sheet(evstd_comp_`ctrl'`cont') sheetreplace firstrow(var)
	
	if `switch_dd'{
		matrix r`ctrl'dd = J(8,39,.)
		matrix r`ctrl'dd = DD_c1 , DD_c2, DD_aa
	
		clear
		matlist r`ctrl'dd
		svmat r`ctrl'dd
					
		export excel "${output}/tables_raw.xlsx", sheet(evstd_comp_`ctrl'`cont'DD) sheetreplace firstrow(var)
	}
}

